<?php
namespace app\common\model;

class Question extends Base
{

    protected $hidden = [
        'create_time','update_time','status'
    ];

    public function questionType()
    {
        return $this->belongsTo(QuestionType::class);
    }

    public function getTypeNameAttr($value,$data)
    {
        $title = QuestionType::find($data['question_type_id'])->title ?? '';
        return $title;
    }

    public function saveChange($data){
        $data['update_time'] = time();
        if(is_numeric($data['id'])){
            return $this->validate(true)->allowField(true)->save($data,[$this->getPk()=>$data['id']]);
        }else{
            $data['create_time'] = time();
            return $this->validate(true)->allowField(true)->save($data);
        }
    }

    public static function getList($map = [])
    {
        return self::where($map)
            ->order('id','desc')
            ->paginate('',false,['query'=>request()->param()]);
    }

}